#include "qipeditor.h"
#include "ui_qipeditor.h"
#include <QList>
#include <QtNetwork/QHostAddress>
#include <QKeyEvent>

QIpEditor::QIpEditor(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QIpEditor)
{
    ui->setupUi(this);
    ui->spinBox1->installEventFilter(this);
    ui->spinBox2->installEventFilter(this);
    ui->spinBox3->installEventFilter(this);
    ui->spinBox4->installEventFilter(this);
}

QIpEditor::~QIpEditor()
{
    delete ui;
}

QString QIpEditor::ipAddressString()
{
    QString ip = QString("%1.%2.%3.%4")
            .arg(ui->spinBox1->value())
            .arg(ui->spinBox2->value())
            .arg(ui->spinBox3->value())
            .arg(ui->spinBox4->value());
    return ip;
}

void QIpEditor::setIpAddress(QString &string)
{
    QStringList list = string.split(".", QString::SkipEmptyParts);
    int i;
    int temp;
    bool changed = false;
    for(i = 0;i < list.count(); i++){
        switch(i){
        case 0:
            temp = list[i].toInt();
            if(temp != ui->spinBox1->value()){
                changed = true;
                ui->spinBox1->setValue(temp);
            }
            break;
        case 1:
            temp = list[i].toInt();
            if(temp != ui->spinBox1->value()){
                changed = true;
                ui->spinBox2->setValue(temp);
            }
            break;
        case 2:
            temp = list[i].toInt();
            if(temp != ui->spinBox1->value()){
                changed = true;
                ui->spinBox3->setValue(temp);
            }
            break;
        case 3:
            temp = list[i].toInt();
            if(temp != ui->spinBox1->value()){
                changed = true;
                ui->spinBox4->setValue(temp);
            }
            break;
        default:
            break;
        }
    }
    if(changed){
        emit ipAddressStringChanged(ipAddressString());
    }
}

void QIpEditor::spinBoxValueChanged()
{
    emit ipAddressStringChanged(ipAddressString());
}

bool QIpEditor::eventFilter(QObject *object, QEvent *event)
{
    QList<QSpinBox *> listOfQSpinBox;
    listOfQSpinBox.append(ui->spinBox1);
    listOfQSpinBox.append(ui->spinBox2);
    listOfQSpinBox.append(ui->spinBox3);
    listOfQSpinBox.append(ui->spinBox4);

    QSpinBox *spinBox = NULL;

    foreach (QSpinBox *box, listOfQSpinBox) {
        if(box == object){
            spinBox = box;
        }
    }

    if(spinBox){
        if(QEvent::FocusIn == event->type()){
            spinBox->selectAll();
            return true;
        }
        if(QEvent::KeyRelease == event->type()){
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            int context = spinBox->value();
            if(keyEvent->key() != Qt::Key_Tab){
                if((context > 25 || context > 99)){
                    this->focusNextChild();
    //                listOfQSpinBox[(listOfQSpinBox.indexOf(spinBox) + 1) % listOfQSpinBox.count()]->setFocus();
                }else if(spinBox->text().length() >= 3){
                    this->focusNextChild();
                }else if(keyEvent->key() < Qt::Key_0 || keyEvent->key() > Qt::Key_9){
                    this->focusNextChild();
                }
            }
        }
    }
    return false;
}
